package com.geodroid.euclidian;




import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.Log;

import com.geodroid.common.kernel.geos.GeoPoint2;


public class DrawPoint extends Drawable {

	
	private static final String TAG = "DrawPoint";
	
	public long priority = 140;
	public RectF circleDraw ;
	public RectF circleHighLighted ;
	private boolean isVisible ; 
	
	
	public float distance(float x, float y){
		return (float) Math.sqrt(Math.pow(screenX -x,2) -Math.pow(screenY -y,2)) ; 
	}
	
	public DrawPoint( GeoPoint2 geo,EuclidianView v){
		super(geo, v); 
		circleDraw = new RectF(); 
		circleHighLighted = new RectF() ;
		update() ;
	}
	


	public void draw(Canvas canvas) { 
		Log.d(TAG, "<draw>") ;
		Log.d(TAG, "<draw> x = "+screenX + "rwY = "+ ((GeoPoint2) getGeo()).inhomX) ;
		Log.d(TAG, "<draw> y = "+screenY + "rwX = "+((GeoPoint2) getGeo()).inhomY) ;
		
		update() ;
		paint.setStyle(Paint.Style.FILL);
	
		if (hightLighted){
			paint.setStrokeWidth(1);
			paint.setColor(Color.RED);
			//canvas.drawCircle(screenX,screenY,1.5f*width,paint) ;
			canvas.drawOval(circleHighLighted, paint);
		}

		paint.setStrokeWidth(1);
		paint.setColor(Color.BLUE);

		//canvas.drawCircle(screenX,screenY,width,paint) ;
		canvas.drawOval(circleDraw, paint); 
		Log.d(TAG, "</draw>") ;
	}
	
	/* (non-Javadoc)
	 * @see com.geodroid.euclidian.Drawable#getDrawingPriority()
	 */
	@Override
	public long getDrawingPriority() {
		// TODO Auto-generated method stub
		return priority;
	}

	public boolean hits(float x, float y){
		
		return (y>=screenY-height && x>=screenX-width && y<=screenY+height && x<screenX+width) ;
	}
	
	public boolean hits(RectF r){
//		circleDraw.sort() ; 
//		r.sort();
		Log.d(TAG,"<hits> RectF" + (circleDraw.contains((int)r.centerX(),(int)r.centerY())||RectF.intersects(circleDraw, r)) ) ;
		Log.d(TAG,"<hits> circleDraw" + circleDraw.toString());
		Log.d(TAG,"<hits> r=" + r.toString());
		Log.d(TAG,"<hits> r.centerX()=" + r.centerX());
		Log.d(TAG,"<hits> r.centerY()=" + r.centerY());
		return (circleDraw.contains(r)||RectF.intersects(circleDraw, r));
	}
	
	public void translate(double ux, double uy){
		Log.d(TAG,"translate") ; 
		double newX = screenX + ux ; 
		double newY = screenY + uy ;
		double rwX = view.toRealWorldCoordX(newX) ; 
		double rwY = view.toRealWorldCoordY(newY) ;
		Log.d(TAG,"moveTo = ("+rwX+","+rwY+")") ;
		GeoPoint2 p = ((GeoPoint2) getGeo()) ; 
		p.setCoords(rwX, rwY) ;
		p.updateCascade() ;
		update() ;
	}
	
	public void update(){
		this.screenX = view.toScreenCoordX(((GeoPoint2) getGeo()).inhomX) ; 
		//this.screenX =  (float) ((GeoPoint2) getGeo()).inhomX ;
		this.screenY = view.toScreenCoordY(((GeoPoint2) getGeo()).inhomY) ;
		//this.screenY =  (float) ((GeoPoint2) getGeo()).inhomY ;
		this.width  =  20f ;
		this.height = 20f ; 
		circleDraw.set(screenX-width/2,screenY-height/2,screenX+width/2,screenY+height/2) ; 
		circleHighLighted.set(circleDraw.centerX()-1.4f*width/2, 
							  circleDraw.centerY()+1.4f*height/2,
							  circleDraw.centerX()+1.4f*width/2,
							  circleDraw.centerY()-1.4f*height/2) ;
	}


	
}
